package cn.lzm.org.service.impl;

import cn.lzm.basic.service.impl.BaseServiceImpl;
import cn.lzm.basic.util.PageList;
import cn.lzm.org.domain.Department;
import cn.lzm.org.mapper.DepartmentMapper;
import cn.lzm.org.query.DepartmentQuery;
import cn.lzm.org.service.IDepartmentService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department>implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

//    @Override
//    public void add(Department department) {
//        departmentMapper.add(department);
//    }
//
//    @Override
//    public void update(Department department) {
//        departmentMapper.update(department);
//    }
//
//    @Override
//    public void delete(Long id) {
//        departmentMapper.delete(id);
//    }
//
//    @Override
//    public List<Department> findAll() {
//        return departmentMapper.findAll();
//    }
//
//    @Override
//    public Department findById(Long id) {
//        return departmentMapper.findById(id);
//    }
//
//    @Override
//    public Integer queryByCount(DepartmentQuery query) {
//        return departmentMapper.queryByCount(query);
//    }
//
//    @Override
//    public PageList<Department> queryByPage(DepartmentQuery query) {
//        if (!StringUtils.isNullOrEmpty(query.getKeyword())) {
//            query.setKeyword(query.getKeyword().trim());
//        }
//        Integer totals = departmentMapper.queryByCount(query);
//        if (null == totals || 0 == totals) {
//            return new PageList<>(0, new ArrayList<>());
//        }
//        // 查询数据
//        List<Department> departments = departmentMapper.queryByPage(query);
////        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&");
////        departments.forEach(System.out::println);
////        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&");
//        //方到返回里面
//        return new PageList<>(totals, departments);
//    }
//
//    @Override
//    public void batchDel(List<Long> ids) {
//        departmentMapper.batchDel(ids);
//
//    }

    @Override
    public List<Department> deptTree() {
        List<Department> allDept = departmentMapper.findAll();
        List<Department> result = new ArrayList<>();
        for (Department department : allDept) {
            if (null == department.getParentId()) {
                result.add(department);
            }else {
                for (Department parent : allDept) {
                    if ( parent.getId().equals(department.getParentId())){
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break;
                    }
                }
            }
        }
        System.out.println(result);
        return result;
    }
}
